package com.sy.simpleegg.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sy.simpleegg.R
import com.sy.simpleegg.bean.RecordOrderBean
import com.sy.simpleegg.databinding.ItemRecordOrderBinding
import com.sy.simpleegg.ext.logFile

class RecordOrderAdapter(
    private var recordList: MutableList<RecordOrderBean>,
    private var context: Context
) :
    RecyclerView.Adapter<RecordOrderAdapter.GoodHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodHolder {
        val binding = ItemRecordOrderBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return GoodHolder(binding)
    }

    override fun getItemCount(): Int = recordList.size ?: 0

    override fun onBindViewHolder(holder: GoodHolder, position: Int) {
        try {
            recordList.let {
                val recordOrderBean = it[position]
                holder.time.text = recordOrderBean.time
                holder.orderAmount.text = "${recordOrderBean.orderAmount.toLong()/100}"
                holder.orderCoins.text = "${recordOrderBean.orderCoins.toLong()}"
                holder.realCoins.text = "${recordOrderBean.realCoins.toLong()}"
                holder.result.text = context.getString(if (recordOrderBean.result) R.string.out_success else R.string.out_err)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            logFile("${javaClass.simpleName} 订单记录数据处理异常：${e.message}")
        }
    }

    fun refreshData(newList: MutableList<RecordOrderBean>){
        recordList = newList
    }

    inner class GoodHolder(binding: ItemRecordOrderBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var time: TextView = binding.tvTime
        var orderAmount: TextView = binding.tvOrderAmount
        var orderCoins: TextView = binding.tvOrderCoins
        var realCoins: TextView = binding.tvRealCoins
        var result: TextView = binding.tvResult
    }

}
